#include "DAO.h"
#include "File.h"
#include "mysql.h"

// /**
//  * @brief 通过文件id删除files表和user_file表
//  * @author 牢大
// */
// void removeFileByIdDAO(MYSQL *conn, File *file){

//     const char *sql = "delete user_file,files from user_file,files where user_file.file_id = ? and files.file_id = ?";

//     MYSQL_STMT *stmt = mysql_stmt_init(conn);


//     mysql_stmt_prepare(stmt, sql, strlen(sql));

//     // 绑定参数
//     MYSQL_BIND bind[2];
//     memset(bind, 0, sizeof(bind));

//     bind[0].buffer_type = MYSQL_TYPE_LONG;
//     bind[0].buffer = (char *)&file->fileid;
//     bind[0].buffer_length = sizeof(int);

//     bind[1].buffer_type = MYSQL_TYPE_LONG;
//     bind[1].buffer = (char *)&file->fileid;
//     bind[1].buffer_length = sizeof(int);

//     if(mysql_stmt_bind_param(stmt, bind)){
//         fprintf(stderr, "mysql_stmt_bind_param() failed\n");
//         mysql_error(conn);
//     }

//     // 执行预处理语句
//     if(mysql_stmt_execute(stmt)){
//         fprintf(stderr, "mysql_stmt_execute() failed\n");
//         mysql_error(conn);
//     }

//     // 释放资源
//     mysql_stmt_close(stmt);

// }


// void updateFileByIdDAO(){



// }


// /**
//  * @param user 要用到uid
//  * @param filename 文件名
//  * @brief 通过文件路径和文件名查找虚拟文件表的文件uid和md5值
//  * @author 牢大
// */
// File* findFileByUidAndFileNameDAO(MYSQL *conn, User *user, char *filename) {
//     const char *sql = "SELECT file_id, file_md5 FROM files WHERE files.file_path LIKE ? AND files.file_name = ?";

//     MYSQL_STMT *stmt = mysql_stmt_init(conn);
//     if (!stmt) {
//         fprintf(stderr, "mysql_stmt_init() failed\n");
//         return NULL;
//     }

//     if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
//         fprintf(stderr, "mysql_stmt_prepare() failed\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     MYSQL_BIND bind[2];
//     memset(bind, 0, sizeof(bind));

//     char user_dir[512];
//     snprintf(user_dir, sizeof(user_dir), "%s%%", user->currentDirectory);

//     bind[0].buffer_type = MYSQL_TYPE_STRING;
//     bind[0].buffer = (char *)user_dir;
//     bind[0].buffer_length = strlen(user_dir);

//     bind[1].buffer_type = MYSQL_TYPE_STRING;
//     bind[1].buffer = (char *)filename;
//     bind[1].buffer_length = strlen(filename);

//     if (mysql_stmt_bind_param(stmt, bind)) {
//         fprintf(stderr, "mysql_stmt_bind_param() failed\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     if (mysql_stmt_execute(stmt)) {
//         fprintf(stderr, "mysql_stmt_execute() failed\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     int fileid = 0;
//     char filemd5[151];
//     memset(filemd5, 0, sizeof(filemd5));
//     MYSQL_BIND result[2];
//     memset(result, 0, sizeof(result));

//     result[0].buffer_type = MYSQL_TYPE_LONG;
//     result[0].buffer = &fileid;
//     result[0].buffer_length = sizeof(fileid);

//     result[1].buffer_type = MYSQL_TYPE_STRING;
//     result[1].buffer = filemd5;
//     result[1].buffer_length = sizeof(filemd5);

//     if (mysql_stmt_bind_result(stmt, result)) {
//         fprintf(stderr, "mysql_stmt_bind_result() failed\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     if (mysql_stmt_store_result(stmt)) {
//         fprintf(stderr, "mysql_stmt_store_result() failed\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     if (mysql_stmt_fetch(stmt) != 0) {
//         // This might return different values, so handle appropriately
//         fprintf(stderr, "mysql_stmt_fetch() failed or no more data\n");
//         mysql_stmt_close(stmt);
//         return NULL;
//     }

//     mysql_stmt_close(stmt);

//     if (fileid == 0) {
//         return NULL;
//     }

//     return FileConstructor(fileid, filename, -1, filemd5, -1);
// }




// /**
//  * @brief 查找是否还有用户在使用这个文件
//  * @author 牢大
// */
// int findFileIsShareDAO(MYSQL *conn, File *file){
//     const char *sql = "select u_id from user_file where file_id = ?";

//     MYSQL_STMT *stmt = mysql_stmt_init(conn);
//     if (stmt == NULL) {
//         mysql_error(conn);
//     }

//     if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
//         mysql_error(conn);
//     }
//     MYSQL_BIND bind[1];
//     memset(bind, 0, sizeof(bind));
//     bind[0].buffer_type = MYSQL_TYPE_LONG;
//     bind[0].buffer = (char *)&file->fileid;
//     bind[0].buffer_length = sizeof(int);

//     mysql_stmt_bind_param(stmt, bind);
//     // 执行预处理语句
//     mysql_stmt_execute(stmt);

//     int uid = 0;
//     MYSQL_BIND result[1];
//     memset(bind, 0, sizeof(result));
//     bind[0].buffer_type = MYSQL_TYPE_LONG;
//     result[0].buffer = (char *)malloc(sizeof(int));
//     result[0].buffer_length = sizeof(int);

//     mysql_stmt_bind_result(stmt, result);
//     // 获取结果
//     mysql_stmt_store_result(stmt);
//     mysql_stmt_fetch(stmt);
//     uid = *(int*)result[0].buffer;
//     // 释放资源
//     mysql_stmt_close(stmt);
//     return uid;
// }

/**
 * @brief 上传文件通用存储过程
*/
void insertIntoFileAndShareDAO(MYSQL *conn, int uid, const char *fileName, long filesize, const char *md5, const char *currentDir){
    char filePath[101];
    memset(filePath, 0, sizeof(filePath));
    snprintf(filePath, sizeof filePath, "%s/%s", currentDir, fileName);

    // 创建存储过程的调用语句
    const char *stmt_str = "call insertIntoFileAndShare(?, ?, ?, ?, ?, ?)";
    
    MYSQL_STMT *stmt = mysql_stmt_init(conn);
    if (!stmt) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    if (mysql_stmt_prepare(stmt, stmt_str, strlen(stmt_str)) != 0) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    MYSQL_BIND bind[6];
    memset(bind, 0, sizeof(bind));

    // 绑定参数
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = (char *)&uid;
    bind[0].is_null = 0;
    bind[0].length = 0;

    bind[1].buffer_type = MYSQL_TYPE_STRING;
    bind[1].buffer = (char *)fileName;
    bind[1].buffer_length = strlen(fileName);
    bind[1].is_null = 0;
    bind[1].length = 0;

    bind[2].buffer_type = MYSQL_TYPE_LONG;
    bind[2].buffer = (char *)&filesize;
    bind[2].is_null = 0;
    bind[2].length = 0;

    bind[3].buffer_type = MYSQL_TYPE_STRING;
    bind[3].buffer = (char *)md5;
    bind[3].buffer_length = strlen(md5);
    bind[3].is_null = 0;
    bind[3].length = 0;

    bind[4].buffer_type = MYSQL_TYPE_STRING;
    bind[4].buffer = (char *)filePath;
    bind[4].buffer_length = strlen(filePath);
    bind[4].is_null = 0;
    bind[4].length = 0;

    bind[5].buffer_type = MYSQL_TYPE_STRING;
    bind[5].buffer = (char *)currentDir;
    bind[5].buffer_length = strlen(currentDir);
    bind[5].is_null = 0;
    bind[5].length = 0;

    // 绑定参数到stmt
    if (mysql_stmt_bind_param(stmt, bind) != 0) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    // 执行存储过程
    if (mysql_stmt_execute(stmt) != 0) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    printf("Stored procedure executed successfully.\n");

    mysql_stmt_close(stmt);
    return;   
}



File *isUserHaveThisFileDAO(MYSQL *conn, const char *fileName, const char *currentDir){
    char filepath[101];
    memset(filepath, 0, sizeof filepath);
    snprintf(filepath, sizeof filepath, "%s/%s", currentDir, fileName);

    const char *sql = "select file_size, file_md5 from files where file_path = ?";

    MYSQL_STMT *stmt = mysql_stmt_init(conn);
    if (stmt == NULL) {
        mysql_error(conn);
    }

    if (mysql_stmt_prepare(stmt, sql, strlen(sql))) {
        mysql_stmt_close(stmt);
        mysql_error(conn);
    }
    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    bind[0].buffer = (char *)&filepath;
    bind[0].buffer_length = strlen(filepath);

    if(mysql_stmt_bind_param(stmt, bind)){
        fprintf(stderr, "mysql_stmt_bind_param() failed\n");
        mysql_stmt_close(stmt);
        return NULL;
    }

    if (mysql_stmt_execute(stmt)) {
        fprintf(stderr, "mysql_stmt_execute() failed\n");
        mysql_stmt_close(stmt);
        return NULL;
    }

    int fileSize = 0;
    char md5[151];
    memset(md5, 0, sizeof md5);

    MYSQL_BIND result[2];
    memset(bind, 0, sizeof(result));

    result[0].buffer_type = MYSQL_TYPE_LONG;
    result[0].buffer = &fileSize;
    result[0].buffer_length = sizeof(fileSize);

    result[1].buffer_type = MYSQL_TYPE_STRING;
    result[1].buffer = md5;
    result[1].buffer_length = sizeof(md5);

    if (mysql_stmt_bind_result(stmt, result)) {
        fprintf(stderr, "mysql_stmt_bind_result() failed\n");
        mysql_stmt_close(stmt);
        return NULL;
    }

    if (mysql_stmt_store_result(stmt)) {
        fprintf(stderr, "mysql_stmt_store_result() failed\n");
        mysql_stmt_close(stmt);
        return NULL;
    }

    if (mysql_stmt_fetch(stmt) != 0) {
        fprintf(stderr, "mysql_stmt_fetch() failed or no more data\n");
        mysql_stmt_close(stmt);
        return NULL;
    }

    mysql_stmt_close(stmt);

    if(fileSize == 0){
        return NULL;
    }

    return createFile(-1, "", fileSize, md5, -1, "");
}